$(document).ready(function() {
	var error = decodeURI(getCookie('error'));
	if(error == "null"||error == ''){
	}else{
		alert(error)
	}
	
	delCookie('error')
	// if($.browser.msie == true && $.browser.version.slice(0,3) < 10) {
	// // placeholder
	// $('input[placeholder]').each(function(){
	// var input = $(this);
	// $(input).val(input.attr('placeholder'));

	// $(input).focus(function(){
	// if (input.val() == input.attr('placeholder')) {
	// input.val('');
	// }
	// });

	// $(input).blur(function(){
	// if (input.val() == '' || input.val() == input.attr('placeholder')) {
	// input.val(input.attr('placeholder'));
	// }
	// });
	// });

	// }
});

// alert
function setAlert(str, timeAlert) {
	if (typeof (timeAlert) == 'undefined') {
		timeAlert = 1500;
	}
	$('.alert-modal').html(str);
	$('.alert-modal').fadeIn();
	// debugger;
	setTimeout(function() {
		$('.alert-modal').fadeOut();
	}, timeAlert);
}

// 提交验证
$('.btn-success').click(function() {
	console.log($('.btn-success').data('code'))
	if (!/^[0-9a-zA-Z]{2,16}$/.test($('.user')[0].value)) {
		// alert('用户名仅允许输入2-16位数字或字母');
		setAlert('用户名仅允许输入2-16位数字或字母');
	} else if (!/^[0-9a-zA-Z]{6,16}$/.test($('.password')[0].value)) {
		// alert('用户名仅允许输入2-16位数字或字母');
		setAlert('密码仅允许输入6-16位数字或字母');
	} else if ($('.validate-input').val() != $('.btn-success').data("code")) {
		// alert('用户名仅允许输入2-16位数字或字母');
		setAlert('请检查验证码是否输入正确');
	} else {
		$('#loginform').attr('action', '/epi/login');
		setCookie('username', $('.user')[0].value, 30);
		
		$('.btn-success').attr("type","submit");
        $("#loginform").submit(function(e){
        	/*console.log(decodeURI(getCookie('error')))
			setAlert(decodeURI(getCookie('error')));*/
          });
        
		/*$('#loginform').submit(function() {
			
		});*/
	}

})

// 写cookies
function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
	var expires = "expires=" + d.toGMTString();
	document.cookie = cname + "=" + cvalue + "; " + expires;
}

// 读取cookies
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i].trim();
		if (c.indexOf(name) == 0)
			return c.substring(name.length, c.length);
	}
	return "";
}

// 删除cookies
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if (cval != null)
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

function getCode() {
	$.ajax({
		url : "/epi/init/RegValCode", // '/epi/staff/staffs/'+thisId,//path+"json/staff.json",
		// data: {
		// id:thisId
		// },
		type : "GET",
		dataType : 'json',
		success : function(data) {
			$('.btn-success').data("code", data.valCode);
		},
		error : function() {
			setAlert('系统繁忙，请刷新重试');
		}
	});
}
